class BillItem {
  int? id;
  final int billId;
  final int productId;
  final String productName;
  final String productSpec;
  final double unitPrice; // 下单时的价格（价格快照）
  final int quantity;
  final double subtotal;

  BillItem({
    this.id,
    required this.billId,
    required this.productId,
    required this.productName,
    required this.productSpec,
    required this.unitPrice,
    required this.quantity,
    required this.subtotal,
  });

  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'bill_id': billId,
      'product_id': productId,
      'product_name': productName,
      'product_spec': productSpec,
      'unit_price': unitPrice,
      'quantity': quantity,
      'subtotal': subtotal,
    };
  }

  factory BillItem.fromMap(Map<String, dynamic> map) {
    return BillItem(
      id: map['id'],
      billId: map['bill_id'],
      productId: map['product_id'],
      productName: map['product_name'],
      productSpec: map['product_spec'],
      unitPrice: map['unit_price'],
      quantity: map['quantity'],
      subtotal: map['subtotal'],
    );
  }

  BillItem copyWith({
    int? id,
    int? billId,
    int? productId,
    String? productName,
    String? productSpec,
    double? unitPrice,
    int? quantity,
    double? subtotal,
  }) {
    return BillItem(
      id: id ?? this.id,
      billId: billId ?? this.billId,
      productId: productId ?? this.productId,
      productName: productName ?? this.productName,
      productSpec: productSpec ?? this.productSpec,
      unitPrice: unitPrice ?? this.unitPrice,
      quantity: quantity ?? this.quantity,
      subtotal: subtotal ?? this.subtotal,
    );
  }
}